using System;
using System.Collections.Generic;
using System.Web;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Ornament.MemberShip;
using Ornament.MemberShip.Permissions;
using Ornament.Messages;
using JinTai.MemorialTablets.Interfaces;

namespace Ornament.Web
{
    /// <summary>
    /// </summary>
    public sealed class OrnamentContext
    {
        /// <summary>
        /// </summary>
        private static readonly OrnamentContext instance = new OrnamentContext();


        /// <summary>
        /// Prevents a default instance of the <see cref="OrnamentContext"/> class from being created. 
        /// </summary>
        private OrnamentContext()
        {
        }

        /// <summary>
        /// Gets Current.
        /// </summary>
        /// <value>
        /// The current context.
        /// </value>
        public static OrnamentContext Current
        {
            get { return instance; }
        }

        /// <summary>
        /// 
        /// </summary>
        public User CurrentUser
        {
            get { return MemberShipFactory.CreateUserDao().Get(HttpContext.Current.User.Identity.Name); }
        }

        /// <summary>
        /// Gets Factory.
        /// </summary>
        /// <value>
        /// The factory.
        /// </value>
        public IMemberShipFactory MemberShipFactory
        {
            get { return Inner.Instance.GetContainer().Resolve<IMemberShipFactory>(); }
        }
        public IMemorialTabletFactory MemorialTablesFactory
        {
            get
            {
                return Inner.Instance.GetContainer().Resolve<IMemorialTabletFactory>();
            }
        }
        public IWebDaoFactory WebFactory
        {
            get
            {
                return Inner.Instance.GetContainer().Resolve<IWebDaoFactory>();
            }
        }
        public static
            Configuration Configuration
        {
            get { return Inner.Instance.GetContainer().Resolve<Configuration>("Configuration"); }
        }

        public InfoManager MessageManager
        {
            get { return Inner.Instance.GetContainer().Resolve<InfoManager>(); }
        }

        public Configuration Configure
        {
            get { return null; }
        }


        public bool CheckPermission(Guid resourceId, Enum operators)
        {
            if (HttpContext.Current.Session != null && HttpContext.Current.Session["Permissions"] == null)
            {
                HttpContext.Current.Session["Permissions"] = CurrentUser.Permissions;
            }
            else
            {
                HttpContext.Current.Session["Permissions"] =
                    MemberShipFactory.CreatePermissionDao().GetPermissions(HttpContext.Current.User.Identity.Name);
            }
            var permissions = HttpContext.Current.Session["Permissions"] as IList<Permission>;
            foreach (Permission permission in permissions)
            {
                if (permission.Resource.Id == resourceId)
                {
                    if (permission.HasOperator(operators))
                        return true;
                }
            }
            return false;
        }

        #region Nested type: Inner

        private class Inner
        {
            public static readonly Inner Instance = new Inner();

            private Inner()
            {
            }

            public IWindsorContainer GetContainer()
            {
                var container = new WindsorContainer(new XmlInterpreter());
                return container;
            }
        }

        #endregion
    }
}